<template>
  <div class="addbox">
      <el-form
        ref="addForm"
        :model="addData"
        :rules="addRules"
        label-width="0px"
        class="add_form"
      >
        <el-form-item prop="adminname">
          <el-input v-model="addData.adminname" prefix-icon="el-icon-user-solid" clearable></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="addData.password" prefix-icon="el-icon-view" type="password" clearable></el-input>
        </el-form-item>
        
        <el-form-item class="add_btn">
          <el-button type="primary" @click="add">添加</el-button>
          
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
    data() {
    return {
      addData: {
        adminname: "",
        password: ""
      },
      addRules: {
        adminname: [
          { required: true, message: "请输入管理员名", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
      add() {
      this.$refs.addForm.validate(valid => {
        if (!valid) return;
        else {
          axios.post("/admins/addOne", this.addData).then(res => {
            if (res.data) {
              this.$message({
                showClose: true,
                message: "增加成功",
                type: "success"
              });
              this.$router.push("/adminList");
            } else {
              this.$message({
                showClose: true,
                message: "请重新输入",
                type: "error"
              });
            }
          });
        }
      });
    }
  }
  
}
</script>

<style>
.addbox {
  width: 500px;
  height: 350px;
  background-color: #fff;
  border-radius: 10px;
}
</style>